package com.cash.loan.modular.cash.controller;

import com.cash.loan.modular.cash.model.LoanRecommend;
import com.cash.loan.modular.cash.result.ServerResponse;
import com.cash.loan.modular.cash.service.RecommendService;
import com.cash.loan.modular.cash.transfer.RecommendDto;
import com.cash.loan.modular.cash.transfer.updateRecommendDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

/**
 * @author Administrator
 * @Date 2018-09-30 14:19
 */
@Controller
@RequestMapping(value = "/cash/recommend")
@Api(description = "推荐位管理")
public class RecommendController {

    private static final String PREFIX = "/cash/recommend";

    @Autowired
    private RecommendService recommendService;

    @RequestMapping(value = "/listPage")
    public String listPage() {
        return PREFIX + "/recommend_list.html";
    }

    @ApiOperation("推荐位列表")
    @GetMapping(value = "/list")
    @ResponseBody
    public ServerResponse list() {
        return ServerResponse.getSuccess("success",recommendService.list());
    }

    @ApiOperation("添加推荐")
    @PostMapping(value = "/add")
    @ResponseBody
    public ServerResponse add(@RequestBody RecommendDto recommend, BindingResult result) {
        if(result.hasErrors()) {
            return ServerResponse.getErrorMessage(result.getAllErrors().toString());
        }
        Boolean addRes = recommendService.add(recommend);
        if(addRes) {
            return ServerResponse.getSuccess("新增成功");
        }
        return ServerResponse.getErrorMessage("新增失败");
    }

    @ApiOperation("修改")
    @PostMapping(value = "/update")
    @ResponseBody
    public ServerResponse update(@RequestBody updateRecommendDto recommend,BindingResult result) {
        if(result.hasErrors()) {
            return ServerResponse.getErrorMessage(result.getAllErrors().toString());
        }
        Boolean updateRes = recommendService.update(recommend);
        if(updateRes) {
            return ServerResponse.getSuccess("修改成功");
        } else {
            return ServerResponse.getErrorMessage("修改失败");
        }
    }

    @ApiOperation("详情")
    @GetMapping(value = "/detail")
    @ResponseBody
    public ServerResponse detail(@RequestParam(value = "id") Integer id) {
        LoanRecommend recommend = recommendService.detail(id);
        return ServerResponse.getSuccess("success",recommend);
    }

    @ApiOperation("删除")
    @PostMapping(value = "/delete")
    @ResponseBody
    public ServerResponse delete(@RequestParam(value = "id") Integer id) {
        Boolean delRes = recommendService.delete(id);
        if(delRes) {
            return ServerResponse.getSuccess("删除成功");
        } else {
            return ServerResponse.getErrorMessage("删除失败");
        }
    }
}
